// result.js
Page({
  data: {
    keyword: '',
    isLoading: true,
    videoList: [],
    currentVideoId: null
  },

  onLoad: function (options) {
    if (options.keyword) {
      const keyword = decodeURIComponent(options.keyword)
      this.setData({
        keyword: keyword
      })

      // 搜索视频
      this.searchVideos(keyword)
    }
  },

  // 监听输入变化
  onInputChange: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  // 执行搜索
  doSearch: function () {
    const keyword = this.data.keyword.trim()
    if (keyword) {
      this.searchVideos(keyword)
    } else {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      })
    }
  },

  // 搜索视频
  searchVideos: function (keyword) {
    this.setData({
      isLoading: true,
      videoList: []
    })

    // 这里应该是调用后端API来搜索视频
    // 为演示目的，使用模拟数据
    setTimeout(() => {
      const mockVideoData = this.getMockVideoData(keyword)

      this.setData({
        videoList: mockVideoData,
        isLoading: false
      })

      // 将关键词添加到搜索历史
      this.saveSearchHistory(keyword)
    }, 1500)
  },

  // 保存搜索历史
  saveSearchHistory: function (keyword) {
    let history = wx.getStorageSync('searchHistory') || []

    // 如果已存在相同关键词，先移除
    const index = history.indexOf(keyword)
    if (index > -1) {
      history.splice(index, 1)
    }

    // 将新关键词添加到历史记录的最前面
    history.unshift(keyword)

    // 只保留最近10条记录
    if (history.length > 10) {
      history = history.slice(0, 10)
    }

    // 保存到本地存储
    wx.setStorageSync('searchHistory', history)
  },

  // 通过标签搜索
  searchTag: function (e) {
    // 接收来自组件的事件
    const tag = e.detail.tag
    this.setData({
      keyword: tag
    })
    this.searchVideos(tag)
  },

  // 返回上一页
  goBack: function () {
    wx.navigateBack()
  },

  // 视频播放事件
  onVideoPlay: function (e) {
    // 接收来自组件的事件
    const videoId = e.detail.id
    const lastVideoId = this.data.currentVideoId

    // 如果有其他视频正在播放，先暂停它
    if (lastVideoId && lastVideoId !== videoId) {
      const videoComponentId = `video-${lastVideoId}`
      const videoContext = wx.createVideoContext(videoComponentId, this)
      videoContext.pause()
    }

    this.setData({
      currentVideoId: videoId
    })
  },

  // 视频暂停事件
  onVideoPause: function () {
    this.setData({
      currentVideoId: null
    })
  },

  // 获取模拟视频数据
  getMockVideoData: function (keyword) {
    // 根据关键词提供不同的模拟数据
    const commonTags = ['家常菜', '简单易做', '美味', '健康饮食']

    if (keyword.includes('红烧肉')) {
      return [
        {
          id: 101,
          title: '经典红烧肉，肥而不腻的做法',
          videoUrl: 'https://example.com/videos/hongshaorou1.mp4',
          coverUrl: '/images/dish1.png',
          author: '厨师王师傅',
          authorAvatar: '/images/avatar1.png',
          playCount: '10.5万',
          likeCount: '5.2万',
          description: '教你做出肥而不腻、入口即化的经典红烧肉，掌握这几个技巧很重要...',
          tags: ['红烧肉', '经典菜式', ...commonTags]
        },
        {
          id: 102,
          title: '30分钟快手红烧肉，新手也能做得很好吃',
          videoUrl: 'https://example.com/videos/hongshaorou2.mp4',
          coverUrl: '/images/dish1_2.png',
          author: '家常菜谱',
          authorAvatar: '/images/avatar2.png',
          playCount: '8.3万',
          likeCount: '4.1万',
          description: '简单快手版红烧肉，只需30分钟，新手也能做出美味可口的红烧肉...',
          tags: ['红烧肉', '快手菜', '下饭菜', ...commonTags]
        },
        {
          id: 103,
          title: '电饭煲红烧肉，懒人必学的一道菜',
          videoUrl: 'https://example.com/videos/hongshaorou3.mp4',
          coverUrl: '/images/dish1_3.png',
          author: '懒人厨房',
          authorAvatar: '/images/avatar3.png',
          playCount: '6.7万',
          likeCount: '3.4万',
          description: '不用看火，不用守着锅，电饭煲一键做出美味红烧肉，懒人必学...',
          tags: ['红烧肉', '电饭煲', '懒人料理', ...commonTags]
        }
      ]
    } else if (keyword.includes('青椒') || keyword.includes('炒肉')) {
      return [
        {
          id: 201,
          title: '农家青椒炒肉，香辣过瘾',
          videoUrl: 'https://example.com/videos/qingjiaochaorou1.mp4',
          coverUrl: '/images/dish2.png',
          author: '乡村小厨',
          authorAvatar: '/images/avatar4.png',
          playCount: '7.2万',
          likeCount: '3.8万',
          description: '农家自制青椒炒肉，香辣过瘾，简单好做又下饭...',
          tags: ['青椒炒肉', '农家菜', '辣味', ...commonTags]
        },
        {
          id: 202,
          title: '青椒炒肉丝，快手家常菜',
          videoUrl: 'https://example.com/videos/qingjiaochaorou2.mp4',
          coverUrl: '/images/dish2_2.png',
          author: '家常美食坊',
          authorAvatar: '/images/avatar5.png',
          playCount: '5.6万',
          likeCount: '2.9万',
          description: '青椒炒肉丝，10分钟搞定一道美味下饭菜，简单快手又好吃...',
          tags: ['青椒炒肉', '肉丝', '快手菜', ...commonTags]
        }
      ]
    } else if (keyword.includes('番茄') || keyword.includes('炒蛋')) {
      return [
        {
          id: 301,
          title: '番茄炒蛋最好吃的做法',
          videoUrl: 'https://example.com/videos/fanqiechaodao1.mp4',
          coverUrl: '/images/dish3.png',
          author: '家常菜谱',
          authorAvatar: '/images/avatar2.png',
          playCount: '9.4万',
          likeCount: '4.7万',
          description: '番茄炒蛋看似简单，但要做好也有诀窍，掌握这几点，保证好吃...',
          tags: ['番茄炒蛋', '家常菜', '经典菜式', ...commonTags]
        },
        {
          id: 302,
          title: '番茄炒蛋的详细步骤，新手也能一次成功',
          videoUrl: 'https://example.com/videos/fanqiechaodao2.mp4',
          coverUrl: '/images/dish3_2.png',
          author: '新手下厨',
          authorAvatar: '/images/avatar6.png',
          playCount: '6.1万',
          likeCount: '3.2万',
          description: '详细步骤教学，新手也能一次做出美味番茄炒蛋，酸甜可口...',
          tags: ['番茄炒蛋', '新手菜', '详细教程', ...commonTags]
        }
      ]
    } else if (keyword.includes('土豆') || keyword.includes('牛肉')) {
      return [
        {
          id: 401,
          title: '香浓土豆炖牛肉，一锅炖出好味道',
          videoUrl: 'https://example.com/videos/tudoudunniurou1.mp4',
          coverUrl: '/images/dish4.png',
          author: '家常菜谱',
          authorAvatar: '/images/avatar2.png',
          playCount: '8.7万',
          likeCount: '4.3万',
          description: '土豆炖牛肉的家常做法，牛肉软烂，土豆入味，简单又美味...',
          tags: ['土豆炖牛肉', '炖菜', '牛肉', ...commonTags]
        },
        {
          id: 402,
          title: '电压力锅土豆炖牛肉，30分钟搞定',
          videoUrl: 'https://example.com/videos/tudoudunniurou2.mp4',
          coverUrl: '/images/dish4_2.png',
          author: '懒人厨房',
          authorAvatar: '/images/avatar3.png',
          playCount: '5.8万',
          likeCount: '2.9万',
          description: '电压力锅版土豆炖牛肉，30分钟搞定，牛肉软烂，土豆香糯...',
          tags: ['土豆炖牛肉', '电压力锅', '快手菜', ...commonTags]
        }
      ]
    } else {
      // 默认返回一些通用菜谱视频
      return [
        {
          id: 501,
          title: keyword + ' 的相关做法',
          videoUrl: 'https://example.com/videos/default1.mp4',
          coverUrl: '/images/default_dish.png',
          author: '美食频道',
          authorAvatar: '/images/avatar1.png',
          playCount: '6.7万',
          likeCount: '3.3万',
          description: '为您找到 ' + keyword + ' 的相关菜谱，简单易学...',
          tags: [keyword, ...commonTags]
        }
      ]
    }
  }
})